//
//  LZImageTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/20.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZImageTableViewCell.h"
#import "Masonry.h"

#import "LZCouponListModel.h"
#import "UIImageView+WebCache.h"

@interface LZImageTableViewCell() <UITextFieldDelegate>

@property (nonatomic, strong) UIImageView * videoImageView;
@property (nonatomic, strong) UITextField * textField;
@property (nonatomic, strong) UIView * line;

@end

@implementation LZImageTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    [self.contentView addSubview:self.videoImageView];
    [self.contentView addSubview:self.textField];
    [self.contentView addSubview:self.line];
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    [self.videoImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(55, 55));
    }];
    
    [self.textField makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.videoImageView.mas_centerY);
        make.leading.mas_equalTo(weakSelf.videoImageView.mas_trailing).with.offset(15);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(55);
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0.5);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
    
}

- (UIImageView *)videoImageView {
    
    if (_videoImageView == nil) {
        _videoImageView = [[UIImageView alloc] init];
    }
    
    return _videoImageView;
}

- (UITextField *)textField {
    
    if (_textField == nil) {
        _textField = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:15];
        _textField.textColor = UIColorFromRGB(0x333333, 1);
        _textField.tintColor = UIColorFromRGB(0x33a928, 1);
        _textField.delegate = self;
        [_textField setValue:UIColorFromRGB(0xcccccc, 1) forKeyPath:@"_placeholderLabel.textColor"];
        [_textField addTarget:self action:@selector(textFieldEditDidEnd:) forControlEvents:UIControlEventEditingDidEnd];
    }
    
    return _textField;
}

- (UIView *)line {
    
    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (void)setCouponListModel:(LZCouponListModel *)couponListModel {
    _couponListModel = couponListModel;
    
    [self.videoImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, couponListModel.videoPhoto]]
                           placeholderImage:[UIImage imageNamed:@"lz_other_placehoiderImage"]];
    
    if (couponListModel.promotionTitle) {
        self.textField.text = couponListModel.promotionTitle;
    }
    else {
        self.textField.text = nil;
        self.textField.placeholder = LZLocalizedString(@"please_enter_promotion_name", @"");
    }
}

- (void)setTextFieldTag:(NSInteger)tag {
    self.textField.tag = tag;
}

- (void)textFieldEditDidEnd:(UITextField *)textField {
    if (self.textFieldEditDidEndBlock) {
        self.textFieldEditDidEndBlock(textField.text,textField.tag,self.indexPath);
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (self.textField == textField)
    {
        if ([toBeString length] > 50) {
            textField.text = [toBeString substringToIndex:50];
            return NO;
        }
    }
    
    return YES;
}

@end
